export default {
  playmusic ({ commit, state }, { index, list }) {
    commit('changesquencelist', list)
    commit('changeplaylist', list)
    commit('changecurrentindex', index)
    commit('toplaying')
    commit('changemode', 0)
    commit('changefullscreen', true)
    commit('addhistorymusic', state.playlist[index])
  },
  // 清除所有歌曲
  clearall ({ commit }) {
    commit('changesquencelist', [])
    commit('changeplaylist', [])
    commit('changecurrentindex', 0)
    commit('tonotplaying')
  },
  // 添加一首歌曲
  addsong ({ commit, state }, song) {
    const playlist = state.playlist.slice()
    const squencelist = state.squencelist.slice()
    const index = playlist.findIndex((item) => item.id === song.id)
    const squenceindex = squencelist.findIndex((item) => item.id === song.id)
    if (index > -1 && squenceindex > -1) {
      commit('changecurrentindex', index)
      commit('toplaying')
      commit('changefullscreen', true)
    } else {
      playlist.push(song)
      squencelist.push(song)
      commit('changesquencelist', squencelist)
      commit('changeplaylist', playlist)
      commit('changecurrentindex', playlist.length - 1)
      commit('toplaying')
      commit('changefullscreen', true)
    }
  },
  // 在播放历史中改变当前播放歌曲
  tohistory ({ state, commit }, { song }) {
    const playlist = state.playlist.slice()
    const squencelist = state.squencelist.slice()
    let index = state.playlist.findIndex((item) => item.id === song.id)
    if (index === -1) {
      playlist.unshift(song)
      squencelist.unshift(song)
      commit('changeplaylist', playlist)
      commit('changesquencelist', squencelist)
      index = 0
    }
    commit('changecurrentindex', index)
    commit('toplaying')
    commit('changefullscreen', true)
    commit('addhistorymusic', song)
  }
}
